package com.blog.mapper;

import com.blog.pojo.User;
import com.blog.pojo.UserInfoVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
@Mapper
public interface UserMapper {

    @Select("select id, username, password, email, created, image, valid from user where email=#{email}")
    UserInfoVO getByEmail(User user);

    /**
     * 注册用函数
     * @param email 邮箱
     * @return 邮箱在表内出现过几次
     */
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    int countByEmail(String email);

    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    int countByUsername(String username);

    @Insert("INSERT INTO user (username, password, email, created, image, valid) VALUES (#{username}, #{password}, #{email}, #{created}, #{image}, #{valid})")
    void insertUser(User user);

    @Select("select id, username, password, email, created, image from user where id=#{id}")
    UserInfoVO getCurrentUser(int id);

    @Select("select id, username, email, created, image from user where id=#{id}")
    UserInfoVO getUserById(Integer id);

    @Select("SELECT COUNT(*) FROM follow WHERE follower_id = #{id}")
    Integer getFocus(Integer id);

    @Select("SELECT COUNT(*) FROM follow WHERE user_id = #{id}")
    Integer getFollower(Integer id);

    @Update("UPDATE user SET valid = 0 WHERE id = #{id}")
    void freezeUser(Integer id);


    @Update("UPDATE user SET valid = 1 WHERE id = #{id}")
    void revertUser(Integer id);

    void updateUser(String username, String image, int id);

    @Update("UPDATE user SET password = #{newPassword} WHERE id = #{id}")
    void updatePassword(int id,String newPassword);

    @Select("SELECT id, username, email, created, image, valid FROM user WHERE id NOT IN (SELECT id FROM admin)")
    List<User> listUsers();

    @Select("SELECT id, username, email, created, image, valid FROM user WHERE username LIKE concat('%',#{condition},'%') AND id NOT IN (SELECT id FROM admin)")
    List<User> listUsersByCondition(String condition);

    @Select("SELECT identity FROM admin WHERE id = #{id}")
    String getIdentity(int id);

    @Update("UPDATE user SET password = #{newPassword} WHERE email = #{email}")
    void changePassword(String email, String newPassword);
}
